//
//  TBWithdrawVc.swift
//  TianTianBiTing
//
//  Created by Gox mac 2 on 2019/2/27.
//  Copyright © 2019 Gox_WallyDiao. All rights reserved.
//

import UIKit

class TBWithdrawVc: TTBaseVC {
    var startPage:Int = 1
    var totalPages:Int = 1
    
    lazy var topLabel:UILabel = {
        let label = UILabel.init(frame: FRAME_CGFloat(17.5, 3, SCREEN_WIDTH - 35, 20))
        label.font = Font_Medium(14)
        label.textColor = RGB(230, 67, 64)
        label.backgroundColor = .clear
        return label
    }()
    
    lazy var centreLabel:UILabel = {
        let label = UILabel.init(frame: FRAME_CGFloat(34.5, 17, SCREEN_WIDTH / 2, 56))
        label.font = Font_Light(40)
        label.textColor = RGB_Same(53)
        label.backgroundColor = WhiteBackColor
        return label
    }()
    
    let TBWithdrawTableViewCellID = "TBWithdrawTableViewCell"
    
    var model = [PkgRecordModel]()
    var modelList: [PkgRecordModel]? {
        didSet {
            guard let model = modelList else { return }
            for dic in model {
                self.model.append(dic)
            }
            
            self.tableView.reloadData()
        }
    }
    
    private lazy var tableView: TTTableView = {
        let tableView = TTTableView.init(frame: FRAME_CGFloat(0, 148, SCREEN_WIDTH, SCREEN_HEIGHT - OFFSET_TOP - 148), style: UITableView.Style.plain)
        tableView.delegate = self
        tableView.dataSource = self
        tableView.backgroundColor = WhiteBackColor
        tableView.separatorStyle = UITableViewCell.SeparatorStyle.none
        tableView.showsVerticalScrollIndicator = false
        tableView.showsHorizontalScrollIndicator = false
        tableView.register(TBWithdrawTableViewCell.self, forCellReuseIdentifier: TBWithdrawTableViewCellID)
        tableView.tableFooterView = UIView.init(frame: .zero)
        return tableView
    }()

    override func viewDidLoad() {
        super.viewDidLoad()

        self.title = "听币明细"
        setTopView()
        setCentreView()
        self.view.addSubview(self.tableView)
        
        setData()
        self.tableView.initMjFooterWithOffest(0) { (taleView) in
            self.startPage += 1
            self.setData()
        }
    }
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        setMoneyData()
        
    }


}

//  MARK: - ###### UI ######
extension TBWithdrawVc {
    func setTopView() {
        SET_LINEVIEW(view: self.view)
        let backView = UIView.init(frame: FRAME_CGFloat(0, 1, SCREEN_WIDTH, 25))
        backView.backgroundColor = RGB(252, 235, 235)
        self.view.addSubview(backView)
        
        
        backView.addSubview(self.topLabel)
    }
    
    func setCentreView() {
        let backView = UIView.init(frame: FRAME_CGFloat(0, 26, SCREEN_WIDTH, 121.5))
        backView.backgroundColor = .white
        self.view.addSubview(backView)
        backView.addSubview(self.centreLabel)
        
        let label = UILabel.init(frame: FRAME_CGFloat(64.5, 80.5, 60, 15))
        label.font = FONT(11)
        label.textColor = RGB_Same(53)
        label.textAlignment = .center
        label.text = "共计(听币)"
        backView.addSubview(label)
        
        let button = UIButton(type: UIButton.ButtonType.custom)
        button.frame = FRAME_CGFloat(SCREEN_WIDTH - 143, 40.5, 95, 40)
        button.setTitle("提现", for: .normal)
        button.setTitleColor(.white, for: .normal)
        button.titleLabel?.font = UIFont.boldSystemFont(ofSize: 19)
        button.layer.cornerRadius = 4
        button.addTarget(self, action: #selector(buyBtnAction), for: .touchUpInside)
        button.backgroundColor = RGB(254, 144, 32)
        backView.addSubview(button)
        
        let lineView = UIView.init(frame: FRAME_CGFloat(0, 120.5, SCREEN_WIDTH, 1))
        lineView.backgroundColor = RGB_Same(240, 1)
        backView.addSubview(lineView)
    }
    
}

//  MARK: - ###### 事件处理 ######
extension TBWithdrawVc {
    func setMoneyData() {
        MineRequestModel.loadWalletBalance(jointArr: [String.getUserId()]) { (success, message, left, right) in
            if success {
                self.topLabel.text = "提示：1听币=1元，可提现\(String.money(right) ?? "0.00")元"
                self.centreLabel.text = String.money(right) ?? "0.00"
            }
        }
    }
    
    func setData() {
        if self.startPage > self.totalPages {
            self.tableView.endAllRefreshing()
            return
        }
         MTProgressHUD.show()
        MineRequestModel.loadPkgRecord(parameters:["startPoint": self.startPage,"maxPage": 20],programId: [String.getUserId(),1]) { (success, message, model, page) in
            self.totalPages = page ?? 1
            if success && model != nil {
                self.modelList = model!
            } else {
                TTAlertView.showMessage(message: message)
            }
            if self.modelList?.count == 0 || self.modelList == nil {
                self.tableView.setNoData(nodata: true)
            } else {
                self.tableView.setNoData(nodata: false)
            }
            MTProgressHUD.hidded(true)
        }
        self.tableView.endAllRefreshing()
    }
    
    @objc func buyBtnAction() {
        let vc = TBWithdrawNextVc()
        PUSHVIEWCONTROLLER(vc)
    }
    
    func typeToStr(num:Int) -> String {
        switch num {
        case 0:
            return "充值红包"
        case 1:
            return "购买红包"
        case 2:
            return "提现红包"
        case 3:
            return "签到红包"
        case 4:
            return "助力红包"
        case 5:
            return "收听时长红包"
        case 6:
            return "定时红包"
        case 7:
            return "邀请好友红包"
        case 8:
            return "唤醒好友红包"
        case 9:
            return "提现失败"
        case 10:
            return "关注公众号送听币红包"
        case 11:
            return "关注微信送听币红包"
        case 12:
            return "支付关注听币红包"
        default:
            return "其他红包"
        }

    }
}

//  MARK: - ###### 事件处理 ######
extension TBWithdrawVc {
    
}

//  MARK: - ###### 事件处理 ######
extension TBWithdrawVc {
    
}

//  MARK: - ###### 协议 ######
extension TBWithdrawVc: UITableViewDelegate, UITableViewDataSource {
    func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return self.model.count
        
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 53
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: TBWithdrawTableViewCellID) as! TBWithdrawTableViewCell
        cell.isUserInteractionEnabled = false
        
        cell.nameLabel.text = typeToStr(num: self.model[indexPath.row].payStatus)
        cell.timeLable.text = self.model[indexPath.row].createTimeStr
        cell.numberLabel.text = "\(String.money(self.model[indexPath.row].balance) ?? "")听币"
        return cell
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        
    }
}
